<script>
import {ref} from 'vue'
import {ElMessage} from 'element-plus'
import {useTokenStore} from '@/stores/token.js'
import request from '@/utils/request.js'
import {useRouter} from 'vue-router'

export default {
  name: 'UserLogin',
  setup() {
    const form = ref({
      username: '',
      password: ''
    })

    const rules = {
      username: [
        {required: true, message: '请输入用户名', trigger: 'blur'},
        {min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur'}
      ],
      password: [
        {required: true, message: '请输入密码', trigger: 'blur'},
        {min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur'}
      ]
    }

    const router = useRouter()
    const tokenStore = useTokenStore()

    const handleLogin = () => {
      request.post('/user/login', form.value)
          .then(response => {
            tokenStore.setToken(response.data.token)
            ElMessage.success('登录成功')
            router.push('/Home/novels')
          })
    }

    const handleRegister = () => {
      router.push('/user/register')
    }

    const handleReset = () => {
      form.value.username = ''
      form.value.password = ''
    }

    return {
      form,
      rules,
      handleLogin,
      handleRegister,
      handleReset
    }
  }
}
</script>

<template>
  <div class="login-container">
    <div class="left-side">
      <img src="@/assets/background.jpg" alt="Background" class="background-image">
      <h1>小说评论网</h1>
    </div>
    <div class="right-side">
      <el-card class="login-card">
        <h2>用户登录</h2>
        <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" @submit.prevent="handleLogin">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" placeholder="请输入用户名" maxlength="16" show-word-limit></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="form.password" placeholder="请输入密码" maxlength="16"
                      show-word-limit></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" native-type="submit">登录</el-button>
            <el-button @click="handleRegister">注册</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  width: 100%;
}

.left-side {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #f0f2f5;
}

.background-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  left: 0;
}

.left-side h1 {
  z-index: 1;
  color: white;
  font-size: 32px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  position: relative;
}

.right-side {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #fff;
}

.login-card {
  width: 400px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style>